//Given two sorted arrays nums1 and nums2 of size m and n respectively, return t
//he median of the two sorted arrays. 
//
// The overall run time complexity should be O(log (m+n)). 
//
// 
// Example 1: 
//
// 
//Input: nums1 = [1,3], nums2 = [2]
//Output: 2.00000
//Explanation: merged array = [1,2,3] and median is 2.
// 
//
// Example 2: 
//
// 
//Input: nums1 = [1,2], nums2 = [3,4]
//Output: 2.50000
//Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
// 
//
// Example 3: 
//
// 
//Input: nums1 = [0,0], nums2 = [0,0]
//Output: 0.00000
// 
//
// Example 4: 
//
// 
//Input: nums1 = [], nums2 = [1]
//Output: 1.00000
// 
//
// Example 5: 
//
// 
//Input: nums1 = [2], nums2 = []
//Output: 2.00000
// 
//
// 
// Constraints: 
//
// 
// nums1.length == m 
// nums2.length == n 
// 0 <= m <= 1000 
// 0 <= n <= 1000 
// 1 <= m + n <= 2000 
// -106 <= nums1[i], nums2[i] <= 106 
// 
// Related Topics 数组 二分查找 分治算法 
// 👍 4072 👎 0


package leetcode.editor.cn;

//Java：Median of Two Sorted Arrays
class P4MedianOfTwoSortedArrays {
    public static void main(String[] args) {
        Solution solution = new P4MedianOfTwoSortedArrays().new Solution();
        System.out.println(solution.findMedianSortedArrays(new int[]{1, 3}, new int[]{1, 2}));
        // TO TEST
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public double findMedianSortedArrays(int[] nums1, int[] nums2) {
            int n1 = nums1.length;
            int n2 = nums2.length;

            // Make sure n1 is always greater than or equal to n2
            if (n1 < n2) {
                return findMedianSortedArrays(nums2, nums1);
            }

            int low = 0;
            int high = n2 * 2;

            while (low <= high) {
                int mid2 = (low + high) / 2;
                int mid1 = n1 + n2 - mid2;

                double l1 = mid1 == 0 ? Integer.MIN_VALUE : nums1[(mid1 - 1) / 2];
                double l2 = mid2 == 0 ? Integer.MIN_VALUE : nums2[(mid2 - 1) / 2];
                double r1 = mid1 == n1 * 2 ? Integer.MAX_VALUE : nums1[mid1 / 2];
                double r2 = mid2 == n2 * 2 ? Integer.MAX_VALUE : nums2[mid2 / 2];

                if (l1 > r2) {
                    low = mid2 + 1;
                } else if (l2 > r1) {
                    high = mid2 - 1;
                } else {
                    return (Math.max(l1, l2) + Math.min(r1, r2)) / 2;
                }
            }

            return -1;
        }

        public double findMedianSortedArrays1(int[] nums1, int[] nums2) {
            int m = nums1.length, n = nums2.length;
            int l = (m + n + 1) / 2;
            int r = (m + n + 2) / 2;
            return (getKth(nums1, 0, nums2, 0, l) + getKth(nums1, 0, nums2, 0, r)) / 2.0;
        }    // 在两个有序数组中二分查找第k大元素

        private int getKth(int[] nums1, int start1, int[] nums2, int start2, int k) {
            // 特殊情况(1)，分析见正文部分
            if (start1 > nums1.length - 1) return nums2[start2 + k - 1];
            if (start2 > nums2.length - 1)
                return nums1[start1 + k - 1];
            // 特征情况(2)，分析见正文部分
            if (k == 1)
                return Math.min(nums1[start1], nums2[start2]);
            // 分别在两个数组中查找第k/2个元素，若存在（即数组没有越界），标记为找到的值；若不存在，标记为整数最大值
            int nums1Mid = start1 + k / 2 - 1 < nums1.length ? nums1[start1 + k / 2 - 1] : Integer.MAX_VALUE;
            int nums2Mid = start2 + k / 2 - 1 < nums2.length ? nums2[start2 + k / 2 - 1] : Integer.MAX_VALUE;
            // 确定最终的第k/2个元素，然后递归查找
            if (nums1Mid < nums2Mid) return getKth(nums1, start1 + k / 2, nums2, start2, k - k / 2);
            else return getKth(nums1, start1, nums2, start2 + k / 2, k - k / 2);
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}